import os

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import pyqtSignal, Qt
from PyQt5.QtGui import QPalette, QColor
from PyQt5.QtWidgets import QApplication, QWidget, QHeaderView, QMainWindow
import resources_rc
from modules import CommonSignal


class MiniWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowMinMaxButtonsHint)
        self.setupUi()
        self.initHandle()

    def setupUi(self):
        self.setObjectName("MainWindow")
        self.resize(360, 60)
        self.setAccessibleName("")
        self.setAccessibleDescription("")
        self.setStyleSheet("#MiniWidget{\n"
                                 "    border:none;\n"
                                 "}\n"
                                 "#MiniCover{\n"
                                 "    border:none;\n"
                                 "}\n"
                                 "#MiniPlayBtn{\n"
                                 "    border-radius:20px;\n"
                                 "    border-style: outset;\n"
                                 "}\n"
                                 "#MiniNextBtn{\n"
                                 "    border-radius:15px;\n"
                                 "    border-style: outset;\n"
                                 "}\n"
                                 "#MiniPreBtn{\n"
                                 "    border-radius:15px;\n"
                                 "    border-style: outset;\n"
                                 "}\n"
                                 "/*\n"
                                 ".QPushButton:hover{\n"
                                 "    background-color:rgba(41,39,39,0.1);\n"
                                 "}\n"
                                 "*/\n"
                                 "#MiniCloseBtn{\n"
                                 "    border: none;\n"
                                 "    background-color: transparent;\n"
                                 "}\n"
                                 "\n"
                                 "#MiniReturnBtn{\n"
                                 "    border: none;\n"
                                 "    background-color: transparent;\n"
                                 "}\n"
                                 "\n"
                                 "#MiniLikeBtn{\n"
                                 "    border-radius:15px;\n"
                                 "    border-style: outset;\n"
                                 "}\n"
                                 "\n"
                                 "#MiniListBtn{\n"
                                 "    border-radius:15px;\n"
                                 "    border-style: outset;\n"
                                 "}\n"
                                 "\n"
                                 "#MiniVolumeBtn{\n"
                                 "    border-radius:15px;\n"
                                 "    border-style: outset;\n"
                                 "}")
        self.MiniWidget = QtWidgets.QWidget(self)
        self.MiniWidget.setMinimumSize(QtCore.QSize(0, 60))
        self.MiniWidget.setMaximumSize(QtCore.QSize(16777215, 60))
        self.MiniWidget.setObjectName("MiniWidget")
        self.MiniLayout = QtWidgets.QHBoxLayout(self.MiniWidget)
        self.MiniLayout.setContentsMargins(0, 0, 0, 0)
        self.MiniLayout.setSpacing(0)
        self.MiniLayout.setObjectName("MiniLayout")
        self.MiniCover = QtWidgets.QFrame(self.MiniWidget)
        self.MiniCover.setMinimumSize(QtCore.QSize(60, 60))
        self.MiniCover.setMaximumSize(QtCore.QSize(60, 60))
        self.MiniCover.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.MiniCover.setFrameShadow(QtWidgets.QFrame.Raised)
        self.MiniCover.setObjectName("MiniCover")
        self.MiniCoverLayout = QtWidgets.QVBoxLayout(self.MiniCover)
        self.MiniCoverLayout.setContentsMargins(0, 0, 0, 0)
        self.MiniCoverLayout.setSpacing(0)
        self.MiniCoverLayout.setObjectName("MiniCoverLayout")
        self.CoverLabel = QtWidgets.QLabel(self.MiniCover)
        self.CoverLabel.setText("")
        self.CoverLabel.setPixmap(QtGui.QPixmap(":/images/images/images/2.jpg"))
        self.CoverLabel.setScaledContents(True)
        self.CoverLabel.setObjectName("CoverLabel")
        self.MiniCoverLayout.addWidget(self.CoverLabel)
        self.MiniLayout.addWidget(self.MiniCover)
        self.MiniPlayer = QtWidgets.QFrame(self.MiniWidget)
        self.MiniPlayer.setMinimumSize(QtCore.QSize(180, 0))
        self.MiniPlayer.setMaximumSize(QtCore.QSize(180, 16777215))
        self.MiniPlayer.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
        self.MiniPlayer.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.MiniPlayer.setFrameShadow(QtWidgets.QFrame.Raised)
        self.MiniPlayer.setObjectName("MiniPlayer")
        self.horizontalLayout = QtWidgets.QHBoxLayout(self.MiniPlayer)
        self.horizontalLayout.setContentsMargins(20, -1, 20, -1)
        self.horizontalLayout.setSpacing(10)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.MiniPreBtn = QtWidgets.QPushButton(self.MiniPlayer)
        self.MiniPreBtn.setMaximumSize(QtCore.QSize(30, 30))
        self.MiniPreBtn.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.MiniPreBtn.setText("")
        icon = QtGui.QIcon()
        icon.addPixmap(QtGui.QPixmap(":/icons/images/icon/play-previous_2.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.MiniPreBtn.setIcon(icon)
        self.MiniPreBtn.setIconSize(QtCore.QSize(30, 30))
        self.MiniPreBtn.setObjectName("MiniPreBtn")
        self.horizontalLayout.addWidget(self.MiniPreBtn)
        self.MiniPlayBtn = QtWidgets.QPushButton(self.MiniPlayer)
        self.MiniPlayBtn.setMaximumSize(QtCore.QSize(40, 40))
        self.MiniPlayBtn.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.MiniPlayBtn.setText("")
        icon1 = QtGui.QIcon()
        icon1.addPixmap(QtGui.QPixmap(":/icons/images/icon/play_6.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.MiniPlayBtn.setIcon(icon1)
        self.MiniPlayBtn.setIconSize(QtCore.QSize(50, 50))
        self.MiniPlayBtn.setObjectName("MiniPlayBtn")
        self.horizontalLayout.addWidget(self.MiniPlayBtn)
        self.MiniNextBtn = QtWidgets.QPushButton(self.MiniPlayer)
        self.MiniNextBtn.setMaximumSize(QtCore.QSize(30, 30))
        self.MiniNextBtn.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.MiniNextBtn.setText("")
        icon2 = QtGui.QIcon()
        icon2.addPixmap(QtGui.QPixmap(":/icons/images/icon/play-next_2.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.MiniNextBtn.setIcon(icon2)
        self.MiniNextBtn.setIconSize(QtCore.QSize(30, 30))
        self.MiniNextBtn.setObjectName("MiniNextBtn")
        self.horizontalLayout.addWidget(self.MiniNextBtn)
        self.MiniLayout.addWidget(self.MiniPlayer)
        self.MiniPanel = QtWidgets.QFrame(self.MiniWidget)
        self.MiniPanel.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.MiniPanel.setFrameShadow(QtWidgets.QFrame.Raised)
        self.MiniPanel.setObjectName("MiniPanel")
        self.MiniPanelLayout = QtWidgets.QHBoxLayout(self.MiniPanel)
        self.MiniPanelLayout.setContentsMargins(0, -1, 0, -1)
        self.MiniPanelLayout.setSpacing(2)
        self.MiniPanelLayout.setObjectName("MiniPanelLayout")
        self.MiniLikeBtn = QtWidgets.QPushButton(self.MiniPanel)
        self.MiniLikeBtn.setMaximumSize(QtCore.QSize(30, 30))
        self.MiniLikeBtn.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.MiniLikeBtn.setAccessibleName("")
        self.MiniLikeBtn.setAccessibleDescription("")
        self.MiniLikeBtn.setText("")
        icon3 = QtGui.QIcon()
        icon3.addPixmap(QtGui.QPixmap(":/icons/images/icon/like.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.MiniLikeBtn.setIcon(icon3)
        self.MiniLikeBtn.setIconSize(QtCore.QSize(24, 24))
        self.MiniLikeBtn.setObjectName("MiniLikeBtn")
        self.MiniPanelLayout.addWidget(self.MiniLikeBtn)
        self.MiniVolumeBtn = QtWidgets.QPushButton(self.MiniPanel)
        self.MiniVolumeBtn.setMaximumSize(QtCore.QSize(30, 30))
        self.MiniVolumeBtn.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.MiniVolumeBtn.setAccessibleName("")
        self.MiniVolumeBtn.setAccessibleDescription("")
        self.MiniVolumeBtn.setText("")
        icon4 = QtGui.QIcon()
        icon4.addPixmap(QtGui.QPixmap(":/icons/images/icon/sound-on_2.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.MiniVolumeBtn.setIcon(icon4)
        self.MiniVolumeBtn.setIconSize(QtCore.QSize(25, 25))
        self.MiniVolumeBtn.setObjectName("MiniVolumeBtn")
        self.MiniPanelLayout.addWidget(self.MiniVolumeBtn)
        self.MiniListBtn = QtWidgets.QPushButton(self.MiniPanel)
        self.MiniListBtn.setMaximumSize(QtCore.QSize(30, 30))
        self.MiniListBtn.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.MiniListBtn.setAccessibleName("")
        self.MiniListBtn.setAccessibleDescription("")
        self.MiniListBtn.setText("")
        icon5 = QtGui.QIcon()
        icon5.addPixmap(QtGui.QPixmap(":/icons/images/icon/list-1.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.MiniListBtn.setIcon(icon5)
        self.MiniListBtn.setIconSize(QtCore.QSize(20, 25))
        self.MiniListBtn.setObjectName("MiniListBtn")
        self.MiniPanelLayout.addWidget(self.MiniListBtn)
        self.MiniLayout.addWidget(self.MiniPanel)
        self.MiniClose = QtWidgets.QFrame(self.MiniWidget)
        self.MiniClose.setMinimumSize(QtCore.QSize(25, 0))
        self.MiniClose.setMaximumSize(QtCore.QSize(25, 16777215))
        self.MiniClose.setFrameShape(QtWidgets.QFrame.StyledPanel)
        self.MiniClose.setFrameShadow(QtWidgets.QFrame.Raised)
        self.MiniClose.setObjectName("MiniClose")
        self.MiniCloseLayout = QtWidgets.QVBoxLayout(self.MiniClose)
        self.MiniCloseLayout.setContentsMargins(1, 2, 1, 45)
        self.MiniCloseLayout.setSpacing(1)
        self.MiniCloseLayout.setObjectName("MiniCloseLayout")
        self.MiniCloseBtn = QtWidgets.QPushButton(self.MiniClose)
        self.MiniCloseBtn.setMaximumSize(QtCore.QSize(20, 20))
        self.MiniCloseBtn.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
        self.MiniCloseBtn.setText("")
        icon6 = QtGui.QIcon()
        icon6.addPixmap(QtGui.QPixmap(":/icons/images/icon/close.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
        self.MiniCloseBtn.setIcon(icon6)
        self.MiniCloseBtn.setObjectName("MiniCloseBtn")
        self.MiniCloseLayout.addWidget(self.MiniCloseBtn)
        self.MiniLayout.addWidget(self.MiniClose)
        self.setCentralWidget(self.MiniWidget)

        self.retranslateUi()
        QtCore.QMetaObject.connectSlotsByName(self)

    def retranslateUi(self):
        _translate = QtCore.QCoreApplication.translate
        self.setWindowTitle(_translate("MiniMainWindow", "MiniMainWindow"))

    def mousePressEvent(self, evt):
        # 获取鼠标当前的坐标
        self.mouse_x = evt.globalX()
        self.mouse_y = evt.globalY()
        print(evt.globalX())
        # 获取窗体当前坐标
        self.origin_x = self.x()
        self.origin_y = self.y()

    # 2.鼠标移动事件
    def mouseMoveEvent(self, evt):
        # print('--------mouseMoveEvent-------')
        # 计算鼠标移动的x，y位移
        if hasattr(self, 'mouse_x'):
            move_x = evt.globalX() - self.mouse_x
            move_y = evt.globalY() - self.mouse_y

            # 计算窗体更新后的坐标：更新后的坐标 = 原本的坐标 + 鼠标的位移
            dest_x = self.origin_x + move_x
            dest_y = self.origin_y + move_y

            # 移动窗体
            self.move(dest_x, dest_y)

    def initHandle(self):
        self.MiniCloseBtn.clicked.connect(CommonSignal.miniWindowCloseSignal)
        self.MiniPlayBtn.clicked.connect(CommonSignal.playSignal)
        self.MiniNextBtn.clicked.connect(CommonSignal.playNextSignal)
        self.MiniPreBtn.clicked.connect(CommonSignal.playPreSignal)

# if __name__ == '__main__':
#     import sys
#
#     app = QApplication(sys.argv)
#
#     # mainWindow = QMainWindow()
#     ui = MiniWindow()
#     # ui.setupUi(mainWindow)
#     ui.show()
#     sys.exit(app.exec())
